/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.util;

/**
 *
 * @author PhucAn
 */
public class PagingUtil {

    public static final int CONST_PAGE_SIZE = 4;
    private  static final int QUICK_PAGE=5;

    public static int getPageNumber(String value) {
        int pageNum = 1;
        if (value == null) {
            return 1;
        } else if (value.trim().equals("")) {
            return 1;
        } else {
            try {
                pageNum = Integer.parseInt(value);
            } catch (NumberFormatException ex) {
                return 1;
            } catch (Exception ex) {
                return 1;
            }
        }
        return pageNum;
    }
    public static String getPageNavigator(int totalRecords, int iPageSize, int iCurrentPageIndex,String requestString)
    {
        int totalPages = 0;
        totalPages= totalRecords / iPageSize;
        if (totalRecords % iPageSize != 0)
        {
            totalPages += 1;
        }       
        
        int i = 0;
        //string str = null;
        String str = "Page:";
        int iCurrentPage = 0;
        int iPageCount = 0;
        int iSpace = 0;
        //int iQuickPage = 0;
        int iStart = 0;
        int iEnd = 0;

        iPageCount = (int)(totalRecords / iPageSize);
        if (totalRecords % iPageSize > 0)
        {
            iPageCount += 1;
        }

        iCurrentPage = iCurrentPageIndex;
        //iQuickPage = QUICK_PAGE;
        if (iCurrentPage % QUICK_PAGE > 0)
        {
            iSpace = (int)(iCurrentPage / QUICK_PAGE) + 1;
        }
        else
        {
            iSpace = (int)(iCurrentPage / QUICK_PAGE);
        }
        if (iPageCount < iSpace * QUICK_PAGE)
        {
            iEnd = (iSpace * QUICK_PAGE) - ((iSpace * QUICK_PAGE) - iPageCount);
        }
        else
        {
            iEnd = (iSpace * QUICK_PAGE);
        }

        if (iEnd < QUICK_PAGE)
        {
            iStart = 1;
        }
        else
        {
            iStart = (iEnd - QUICK_PAGE) + 1;
        }

        if (iCurrentPage > QUICK_PAGE)
        {
            if (iStart - 1 == iCurrentPage)
            {
                str = str + "...";
            }
            else
            {
                str += " " + "<a href = '" + requestString + (iStart - 1) + "'>" + "Prev " + "</a>";
            }
        }
        for (i = iStart; i <= iEnd; i++)
        {
            if (i == iCurrentPage)
            {
                str += " " + i;
            }
            else
            {
                str += " " + "<a href = '" + requestString + i + "'>" + i + "</a>";
            }
        }
        if (iCurrentPage < iPageCount & iEnd < iPageCount)
        {
            if (iCurrentPage == iEnd + 1)
            {
                str = " " + str + "...";
            }
            else
            {
                str += " " + "<a" + " href = '" + requestString + (iEnd + 1) + "'>" + "Next" + "</a>";
            }
        }
        str += ("<span style='margin-left:10px;'> of " + String.valueOf( totalPages) + " pages</span>");
        return str;
    }

}
